<?php
namespace Api\Controller;
use Think\Controller;
class DeliveryController extends PublicController {

    /**
     * 赠送用户详情页
     * @author tiedong
     * @date 2018-01-23
     */
    public function index(){
        $user_id=intval($this->get("id"));
        if (!$user_id){
            $this->json_error("网络异常");
            exit();
        }
        $order_id = !empty($_GET['order_id'])?intval($_GET['order_id']) : '' ;
        if ( empty($order_id)) {
            $this->json_error("参数有误！！！");
            exit;
        }
    	//所有地址
    	$order = D("Order")->where('id='.$order_id)->field("id,order_sn,shop_id,vid,product_num,amount")->find();
        $order['name'] = M("brand")->where('id='.$order['shop_id'])->getField("name");
        $order['photo_image'] = __DATAURL__."UploadFiles/".M("brand_image")->where('id='.$order['vid'])->getField("brand_photo");
        $order['product'] = M("order_product")->where('order_id='.$order['id'])->field('id,price,num,name,photo_x')->select();
        foreach ($order['product'] as $key=>$value) {
            $order['product'][$key]['photo_x'] = __DATAURL__.$value['photo_x'];
        }
        $this->json_result($order);
		exit();
    }


    /**
     * 用户转发保存用户的留言
     * @author tiedong
     * @date 2018-01-23
     */
    public function save_content(){
        $user_id=intval($this->get("id"));
        if (!$user_id){
            echo json_encode(array('status'=>0,'err'=>'网络异常.'.__LINE__));
            exit();
        }
        $order_id = !empty($_POST['order_id'])?intval($_POST['order_id']) : '' ;
        if ( empty($order_id)) {
            $this->json_error("参数有误！！！");
            exit;
        }
        $data = array();
        $data['remark'] = htmlspecialchars($_POST['remark']);
        M("order")->where('id='.$order_id)->save($data);
    }

    /**
     * 分享用户心意卡详情页面
     * @author tiedong
     * @date 2018-01-22
     */
    public function brand_details(){
        $user_id=intval($this->get("id"));
        if (!$user_id){
            $this->json_error("网络异常");
            exit();
        }
        $order_id = !empty($_GET['order_id'])?intval($_GET['order_id']) : '' ;
        if ( empty($order_id)) {
            $this->json_error("参数有误！！！");
            exit;
        }
        //所有地址
        $order = D("Order")->where('id='.$order_id)->field("id,order_sn,shop_id,vid,product_num,amount,remark")->find();
        $order['name'] = M("brand")->where('id='.$order['shop_id'])->getField("name");
        $order['photo_image'] = __DATAURL__."UploadFiles/".M("brand_image")->where('id='.$order['vid'])->getField("brand_photo");
        $order['product'] = M("order_product")->where('order_id='.$order['id'])->field('id,price,num,name,photo_x')->select();
        foreach ($order['product'] as $key=>$value) {
            $order['product'][$key]['photo_x'] = __DATAURL__.$value['photo_x'];
        }
        $this->json_result($order);
        exit();
    }

    /**
     * 用户修改地址
     * @author tiedong
     * @date 2018-01-23
     */
    public function modifyAddress()
    {
        $user_id=intval($this->get("id"));
        if (!$user_id){
            echo json_encode(array('status'=>0,'err'=>'网络异常.'.__LINE__));
            exit();
        }
        //查找该用户是否已经存在默认地址
        $check_default = M('address')->where("uid={$user_id} and is_default=1")->getField('id');
        if ($check_default) {
            $this->json_error("默认地址已经存在！");
            exit();
        }
        $addr_id = $_POST['addr_id'];
        $data = array();
        $data['name'] = trim($_POST['receiver']);
        $data['tel'] = trim($_POST['tel']);
        $data['region_name'] = $_POST['region_name'];
        $data['address'] = $_POST['adds'];
        //$data['code'] = $_POST['code'];
        $data['uid'] = intval($user_id);
        if (!$data['name'] || !$data['tel'] || !$data['address']) {
            $this->json_error("请完善信息后再提交！！！");
            exit();
        }
        $data['is_default'] = intval($_POST['is_default']);
        $res = M('address')->where('id='.$addr_id)->save($data);
        if ($res) {
            $this->json_result("修改地址成功！");
            exit();
        }else{
            $this->json_error("修改失败！");
            exit();
        }
    }

    /**
     * 户点击领取赠送心意卡
     * @author tiedong
     * @date 2018-01-23
     */
    public function receiveCard(){
        $user_id=intval($this->get("id"));
        if (!$user_id){
            echo json_encode(array('status'=>0,'err'=>'网络异常.'.__LINE__));
            exit();
        }
        $order_id = !empty($_POST['order_id']) ? intval($_POST['order_id']) : 0 ;
        if ( empty($order_id) ) {
            $this->json_error("该订单不存在!");
            exit;
        }
        $checkSuId = M("order")->where("id=".$order_id)->getField("suId");
        if ( $checkSuId ) {
            $this->json_error("该心意卡有人领取了");
            exit;
        }
        $uId = M("order")->where("id=".$order_id)->getField("uid");
        if ( $uId==$user_id ) {
            $this->json_error("不能领取自己购买的心意卡！");
            exit;
        }
        $sql = " update lr_order set suId={$user_id} WHERE id=".$order_id;
        $result = M("order")->execute($sql);
        if ( $result ) {
            $this->json_result("心意卡领取成功！");
            exit;
        } else {
            $this->json_error("心意卡领取失败！");
            exit;
        }
    }


}